﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcs.Web.Admin.distribution
{
    public partial class mystorelist : Web.Admin.BasePage
    {
        lcs.BLL.distribution bll = new lcs.BLL.distribution();
        public string dogid = "NO";
        public string Decode(string str)
        {

            if (dogid == "NO")
            {
                if (Request.Cookies["dogid"]!=null)
                    dogid = Request.Cookies["dogid"].Value.ToString();
            }
            return DecodeDog(str, dogid);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                
                    BindData();
                    Page.DataBind();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        #region gridView
        public void BindData()
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" and uid="+getid());
            if (ddltype.SelectedValue == "1")
                strWhere.Append(" and (dstate>=0 and dstate<2 or dstate is null) ");
            if (ddltype.SelectedValue == "2")
                strWhere.Append(" and dstate=2");
            if (ddltype.SelectedValue == "3")
                strWhere.Append(" and dstate=-1");

            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and material like '%{0}%'", txtKeyword.Text.Trim());
            }
            if (txtKeyword2.Text.Trim() != "")
            {
                if (Decode("1") == "1")
                strWhere.AppendFormat(" and projectname like '%{0}%'", txtKeyword2.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txt_sname.Text))
            {
                strWhere.AppendFormat(" and sname like '%{0}%'", txt_sname.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtpurchaseid.Text))
            {
                strWhere.AppendFormat(" and purchaseid like '%{0}%'", txtpurchaseid.Text.Trim());
            }
            ds = bll.GetListv(strWhere.ToString());
            gridView.DataSource = ds;
            gridView.DataBind();
        }        
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //string t1 = e.Row.Cells[15].Text;
                string text = "";
                Label l = e.Row.FindControl("lbldstatecn") as Label;
                if (l != null) text = l.Text;

                if (text.Replace("&nbsp;","") != "")
                {
                    e.Row.Cells[0].Controls[0].Visible=false;
                }
                if (text != "数据确认")
                {
                    e.Row.Cells[1].Controls[0].Visible =false;
                }
                object obj1 = DataBinder.Eval(e.Row.DataItem, "dstatecn");
                if ((obj1 != null) && ((obj1.ToString() == "已过期")))
                {
                    e.Row.Attributes.Add("class", "red");
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
           
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delc")
            {
                int index = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;

                //通过下面的方式找到当前行主键
                int id = int.Parse(gridView.DataKeys[index].Value.ToString());
                lcs.BLL.distribution bll = new lcs.BLL.distribution();                        
                bll.delaognum(id);                
                Maticsoft.BLL.monthSupplayPlan mb = new Maticsoft.BLL.monthSupplayPlan();
                mb.deldate(0, id);               
            }
            if (e.CommandName == "dely")
            {
                int index = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;

                //通过下面的方式找到当前行主键                
                int id = int.Parse(gridView.DataKeys[index].Value.ToString());
                int nums = new BLL.materialinfo().GetCount(id);
                if (nums == 0)
                {
                    lcs.BLL.distribution bll = new lcs.BLL.distribution();
                    bll.delpinum(id);
                    Maticsoft.BLL.monthSupplayPlan mb = new Maticsoft.BLL.monthSupplayPlan();
                    mb.deldate(1, id);
                    new BLL.materialinfo().Deletedid(id);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('已入库不能修改！');", true);
                }

            }
            BindData();
        }
    }
}